package com.yy.bolg.controller.workOrder;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yy.bolg.constant.ResultCode;
import com.yy.bolg.entity.*;
import com.yy.bolg.request.TestingAccessoryRequest;
import com.yy.bolg.request.WorkOrderRequest;
import com.yy.bolg.resp.*;
import com.yy.bolg.service.*;
import com.yy.bolg.util.CodeGenerateUtil;
import com.yy.bolg.util.DateUtil;
import com.yy.bolg.util.RespenseJsonTransformationUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 工单控制器
 * @author enmaoFu
 * @date 2024-11-11
 * RequestMapping("/workOrder") 是说明这个类的访问地址是 /workOrder
 */
@Api(tags = {"工单"})
@Controller
@RequestMapping(value = "/workOrder")
public class WorkOrderController {

    @Autowired
    private WorkOrderService service;// 自动载入Service对象
    @Autowired
    private TestingService testingService;// 自动载入Service对象
    @Autowired
    private TestingAccessoryService testingAccessoryService;// 自动载入Service对象
    @Autowired
    private RepairService repairService;// 自动载入Service对象
    @Autowired
    private CustomerService customerService;// 自动载入Service对象
    @Autowired
    private ElectricianService electricianService;// 自动载入Service对象
    @Autowired
    private EvaluateService evaluateService;// 自动载入Service对象

    /**
     * 生成工单
     * 服务器访问路径：http://localhost:8080/bolg/workOrder/createWorkOrder
     * 本地访问路径：http://localhost:8111/workOrder/createWorkOrder
     * 参数：
     * {"feedbackCode":"FEED20241102142035","customerCode":"CUS20241102102456","customerName":"重庆市场监督管理局","customerUserName":"张三","customerUserPhone":"15123059598","adminUserCode":"ADMIN-USER20241031211423","workOrderContent":"这个灯时好时坏","workOrderImgFile":"https://img2.baidu.com/it/u=2384266343,2050170443&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500,https://img2.baidu.com/it/u=2384266343,2050170443&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500","workOrderVideoFile":"视频路径","workOrderVoiceFile":"音频路径","feedbackDate":"2024-11-02 14:20:35","feedbackProvince":"重庆市","feedbackCity":"重庆市","feedbackRegion":"九龙坡区","feedbackAddress":"江厦星光汇2楼","feedbackLongitude":132.121,"feedbackLatitude":28.123,"feedbackType":1,"workOrderCreateRemarks":"总工填写的描述"}
     */
    @ApiOperation(value = "生成工单", notes = "参数：{\"feedbackCode\":\"FEED20241102142035\",\"customerCode\":\"CUS20241102102456\",\"customerName\":\"重庆市场监督管理局\",\"customerUserName\":\"张三\",\"customerUserPhone\":\"15123059598\",\"adminUserCode\":\"ADMIN-USER20241031211423\",\"workOrderContent\":\"这个灯时好时坏\",\"workOrderImgFile\":\"https://img2.baidu.com/it/u=2384266343,2050170443&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500,https://img2.baidu.com/it/u=2384266343,2050170443&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500\",\"workOrderVideoFile\":\"视频路径\",\"workOrderVoiceFile\":\"音频路径\",\"feedbackDate\":\"2024-11-02 14:20:35\",\"feedbackProvince\":\"重庆市\",\"feedbackCity\":\"重庆市\",\"feedbackRegion\":\"九龙坡区\",\"feedbackAddress\":\"江厦星光汇2楼\",\"feedbackLongitude\":132.121,\"feedbackLatitude\":28.123,\"feedbackType\":1,\"workOrderCreateRemarks\":\"总工填写的描述\"}")
    @RequestMapping(value = "/createWorkOrder",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String createWorkOrder(@RequestBody WorkOrderRequest request){
        String result;

        WorkOrderBean workOrderBean = new WorkOrderBean();
        workOrderBean.setPsWorkOrderCode(CodeGenerateUtil.getWorkOrderCode());
        workOrderBean.setPsFeedbackCode(request.getFeedbackCode());
        workOrderBean.setPsCustomerCode(request.getCustomerCode());
        workOrderBean.setPsCustomerName(request.getCustomerName());
        workOrderBean.setPsCustomerUserName(request.getCustomerUserName());
        workOrderBean.setPsCustomerUserPhone(request.getCustomerUserPhone());
        workOrderBean.setPsAdminUserCode(request.getAdminUserCode());
        workOrderBean.setPsWorkOrderContent(request.getWorkOrderContent());
        workOrderBean.setPsWorkOrderImgFile(request.getWorkOrderImgFile());
        workOrderBean.setPsWorkOrderVideoFile(request.getWorkOrderVideoFile());
        workOrderBean.setPsWorkOrderVoiceFile(request.getWorkOrderVoiceFile());
        workOrderBean.setPsFeedbackDate(request.getFeedbackDate());
        workOrderBean.setPsFeedbackProvince(request.getFeedbackProvince());
        workOrderBean.setPsFeedbackCity(request.getFeedbackCity());
        workOrderBean.setPsFeedbackRegion(request.getFeedbackRegion());
        workOrderBean.setPsFeedbackAddress(request.getFeedbackAddress());
        workOrderBean.setPsFeedbackLongitude(request.getFeedbackLongitude());
        workOrderBean.setPsFeedbackLatitude(request.getFeedbackLatitude());
        workOrderBean.setPsFeedbackType(request.getFeedbackType());
        workOrderBean.setPsWorkOrderTestingState(2);
        workOrderBean.setPsWorkOrderRepairState(2);
        workOrderBean.setPsWorkOrderType(request.getWorkOrderType());
        workOrderBean.setPsWorkOrderCreateRemarks(request.getWorkOrderCreateRemarks());

        try {
            service.add(workOrderBean);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "生成工单成功", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 查询工单详情
     * 服务器访问路径：http://localhost:8080/bolg/workOrder/getWorkOrderInfo
     * 本地访问路径：http://localhost:8111/workOrder/getWorkOrderInfo
     * 参数：
     * {"workOrderCode":"WROR20241111181240"}
     */
    @ApiOperation(value = "查询工单详情", notes = "参数：{\"workOrderCode\":\"WROR20241111181240\"}", response = WorkOrderDetailsResp.class)
    @RequestMapping(value = "/getWorkOrderInfo",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String getWorkOrderInfo(@RequestBody WorkOrderRequest request){
        String result;

        WorkOrderBean workOrderBean = new WorkOrderBean();
        workOrderBean.setPsWorkOrderCode(request.getWorkOrderCode());

        try {
            WorkOrderBean returnWorkOrderBean = service.findOneByCode(workOrderBean);
            if (null != returnWorkOrderBean) {
                WorkOrderDetailsResp resp = new WorkOrderDetailsResp();
                // 工单编号
                resp.setWorkOrderCode(returnWorkOrderBean.getPsWorkOrderCode());
                // 上报编号
                resp.setFeedbackCode(returnWorkOrderBean.getPsFeedbackCode());
                // 客户（单位）编号
                resp.setCustomerCode(returnWorkOrderBean.getPsCustomerCode());
                // 客户（单位）名称
                resp.setCustomerName(returnWorkOrderBean.getPsCustomerName());
                // 客户（单位）管理人姓名
                resp.setCustomerUserName(returnWorkOrderBean.getPsCustomerUserName());
                // 客户（单位）管理人手机号
                resp.setCustomerUserPhone(returnWorkOrderBean.getPsCustomerUserPhone());
                // 电工编号
                resp.setElectricianCode(returnWorkOrderBean.getPsElectricianCode());
                // 电工姓名
                resp.setElectricianName(returnWorkOrderBean.getPsElectricianName());
                // 电工手机号
                resp.setElectricianPhone(returnWorkOrderBean.getPsElectricianPhone());
                // 总工编号
                resp.setAdminUserCode(returnWorkOrderBean.getPsAdminUserCode());
                // 上报文字描述
                resp.setWorkOrderContent(returnWorkOrderBean.getPsWorkOrderContent());
                // 上报语音描述路径
                resp.setWorkOrderVoiceFile(returnWorkOrderBean.getPsWorkOrderVoiceFile());
                // 上报视频描述路径
                resp.setWorkOrderVideoFile(returnWorkOrderBean.getPsWorkOrderVideoFile());
                // 上报图片描述路径
                resp.setWorkOrderImgFile(returnWorkOrderBean.getPsWorkOrderImgFile());
                // 上报时间
                resp.setFeedbackDate(returnWorkOrderBean.getPsFeedbackDate());
                // 上报客户（单位）地区-省
                resp.setFeedbackProvince(returnWorkOrderBean.getPsFeedbackProvince());
                // 上报客户（单位）地区-市
                resp.setFeedbackCity(returnWorkOrderBean.getPsFeedbackCity());
                // 上报客户（单位）地区-区
                resp.setFeedbackRegion(returnWorkOrderBean.getPsFeedbackRegion());
                // 上报客户（单位）-详细地址
                resp.setFeedbackAddress(returnWorkOrderBean.getPsFeedbackAddress());
                // 上报客户（单位）经度
                resp.setFeedbackLongitude(returnWorkOrderBean.getPsFeedbackLongitude());
                // 上报客户（单位）纬度
                resp.setFeedbackLatitude(returnWorkOrderBean.getPsFeedbackLatitude());
                // 上报类型
                resp.setFeedbackType(returnWorkOrderBean.getPsFeedbackType());
                // 工单类型
                resp.setWorkOrderType(returnWorkOrderBean.getPsWorkOrderType());
                // 工单派遣/发布时间
                resp.setWorkOrderEffectDate(returnWorkOrderBean.getPsWorkOrderEffectDate());
                // 工单要求响应时间（小时数）
                resp.setWorkOrderRequirementDate(returnWorkOrderBean.getPsWorkOrderRequirementDate());
                // 工单接受或抢单时间
                resp.setWorkOrderAcceptDate(returnWorkOrderBean.getPsWorkOrderAcceptDate());
                // 总工生成工单备注
                resp.setWorkOrderCreateRemarks(returnWorkOrderBean.getPsWorkOrderCreateRemarks());
                // 电工到场打卡-省
                resp.setWorkOrderArriveProvince(returnWorkOrderBean.getPsWorkOrderArriveProvince());
                // 电工到场打卡-市
                resp.setWorkOrderArriveCity(returnWorkOrderBean.getPsWorkOrderArriveCity());
                // 电工到场打卡-区
                resp.setWorkOrderArriveRegion(returnWorkOrderBean.getPsWorkOrderArriveRegion());
                // 电工到场打卡-详细地址
                resp.setWorkOrderArriveAddress(returnWorkOrderBean.getPsWorkOrderArriveAddress());
                // 电工到场打卡经度
                resp.setWorkOrderArriveLongitude(returnWorkOrderBean.getPsWorkOrderArriveLongitude());
                // 电工到场打卡纬度
                resp.setWorkOrderArriveLatitude(returnWorkOrderBean.getPsWorkOrderArriveLatitude());
                // 电工到场打卡图片路径
                resp.setWorkOrderArriveImg(returnWorkOrderBean.getPsWorkOrderArriveImg());
                // 电工到场打卡时间
                resp.setWorkOrderArriveDate(returnWorkOrderBean.getPsWorkOrderArriveDate());
                // 是否检测完成
                resp.setWorkOrderTestingState(returnWorkOrderBean.getPsWorkOrderTestingState());
                // 是否维修完成
                resp.setWorkOrderRepairState(returnWorkOrderBean.getPsWorkOrderRepairState());
                // 检测编号
                resp.setTestingCode(returnWorkOrderBean.getPsTestingCode());
                // 维修编号
                resp.setRepairCode(returnWorkOrderBean.getPsRepairCode());
                // 工单状态
                resp.setWorkOrderState(returnWorkOrderBean.getPsWorkOrderState());
                // 工单生成时间
                resp.setWorkOrderCreateDate(returnWorkOrderBean.getCreateDate());
                // 检测结果
                if(!StringUtils.isEmpty(returnWorkOrderBean.getPsTestingCode())){
                    TestingBean testingBean = new TestingBean();
                    testingBean.setPsTestingCode(returnWorkOrderBean.getPsTestingCode());

                    TestingBean returnTestingBean = testingService.findOneByCode(testingBean);
                    if (null != returnTestingBean){
                        TestingResp tr = new TestingResp();
                        tr.setTestingCode(returnTestingBean.getPsTestingCode());
                        tr.setCustomerCode(returnTestingBean.getPsCustomerCode());
                        tr.setFeedbackCode(returnTestingBean.getPsFeedbackCode());
                        tr.setWorkOrderCode(returnTestingBean.getPsWorkOrderCode());
                        tr.setTestingContent(returnTestingBean.getPsTestingContent());
                        tr.setTestingVoiceFile(returnTestingBean.getPsTestingVoiceFile());
                        tr.setTestingImgFile(returnTestingBean.getPsTestingImgFile());
                        tr.setTestingAgree(returnTestingBean.getPsTestingAgree());
                        tr.setTestingAgreeImg(returnTestingBean.getPsTestingAgreeImg());
                        tr.setTestingTotal(returnTestingBean.getPsTestingTotal());

                        TestingAccessoryBean testingAccessoryBean = new TestingAccessoryBean();
                        testingAccessoryBean.setPsTestingCode(returnWorkOrderBean.getPsTestingCode());

                        List<TestingAccessoryBean> testingAccessoryBeans = testingAccessoryService.findAllByCondition(testingAccessoryBean);
                        List<TestingAccessoryRequest> testingAccessoryRequests = new ArrayList<>();
                        for(TestingAccessoryBean tabItem: testingAccessoryBeans){
                            TestingAccessoryRequest testingAccessoryRequest = new TestingAccessoryRequest();
                            testingAccessoryRequest.setTestingCode(tabItem.getPsTestingCode());
                            testingAccessoryRequest.setAccessoryCode(tabItem.getPsAccessoryCode());
                            testingAccessoryRequest.setAccessoryName(tabItem.getPsAccessoryName());
                            testingAccessoryRequest.setAccessoryPrice(tabItem.getPsAccessoryPrice());
                            testingAccessoryRequest.setAccessoryClassCode(tabItem.getPsAccessoryClassCode());
                            testingAccessoryRequest.setAccessoryClassName(tabItem.getPsAccessoryClassName());
                            testingAccessoryRequest.setTestingAccessoryNumber(tabItem.getPsTestingAccessoryNumber());
                            testingAccessoryRequests.add(testingAccessoryRequest);
                        }
                        tr.setTarList(testingAccessoryRequests);
                        resp.setTestingResp(tr);
                    }
                }
                // 维修结果
                if(!StringUtils.isEmpty(returnWorkOrderBean.getPsRepairCode())){
                    RepairBean repairBean = new RepairBean();
                    repairBean.setPsRepairCode(returnWorkOrderBean.getPsRepairCode());

                    RepairBean returnRepairBean = repairService.findOneByCode(repairBean);
                    if (null != returnRepairBean){
                        RepairResp rr = new RepairResp();
                        rr.setRepairCode(returnRepairBean.getPsRepairCode());
                        rr.setCustomerCode(returnRepairBean.getPsCustomerCode());
                        rr.setFeedbackCode(returnRepairBean.getPsFeedbackCode());
                        rr.setWorkOrderCode(returnRepairBean.getPsWorkOrderCode());
                        rr.setRepairContent(returnRepairBean.getPsRepairContent());
                        rr.setRepairVoiceFile(returnRepairBean.getPsRepairVoiceFile());
                        rr.setRepairImgFile(returnRepairBean.getPsRepairImgFile());
                        resp.setRepairResp(rr);
                    }
                }
                // 单位信息
                if(!StringUtils.isEmpty(returnWorkOrderBean.getPsCustomerCode())){
                    CustomerBean customerBean = new CustomerBean();
                    customerBean.setPsCustomerCode(returnWorkOrderBean.getPsCustomerCode());
                    CustomerBean returnCustomerBean = customerService.findOneByCode(customerBean);
                    if (null != returnCustomerBean) {
                        CustomerResp cr = new CustomerResp();
                        cr.setCustomerCode(returnCustomerBean.getPsCustomerCode());
                        cr.setCustomerUserHead(returnCustomerBean.getPsCustomerUserHead());
                        cr.setCustomerUserName(returnCustomerBean.getPsCustomerUserName());
                        cr.setCustomerUserPhone(returnCustomerBean.getPsCustomerUserPhone());
                        cr.setCustomerUserPwd(returnCustomerBean.getPsCustomerUserPwd());
                        cr.setCustomerUserPosition(returnCustomerBean.getPsCustomerUserPosition());
                        cr.setCustomerName(returnCustomerBean.getPsCustomerName());
                        cr.setCustomerProvince(returnCustomerBean.getPsCustomerProvince());
                        cr.setCustomerCity(returnCustomerBean.getPsCustomerCity());
                        cr.setCustomerRegion(returnCustomerBean.getPsCustomerRegion());
                        cr.setCustomerAddress(returnCustomerBean.getPsCustomerAddress());
                        cr.setCustomerLongitude(returnCustomerBean.getPsCustomerLongitude());
                        cr.setCustomerLatitude(returnCustomerBean.getPsCustomerLatitude());
                        cr.setCustomerCategory(returnCustomerBean.getPsCustomerCategory());
                        cr.setCustomerLicense(returnCustomerBean.getPsCustomerLicense());
                        cr.setCustomerEmergencyUserName(returnCustomerBean.getPsCustomerEmergencyUserName());
                        cr.setCustomerEmergencyUserPhone(returnCustomerBean.getPsCustomerEmergencyUserPhone());
                        cr.setCustomerState(returnCustomerBean.getPsCustomerState());
                        cr.setCustomerSaleName(returnCustomerBean.getPsCustomerSaleName());
                        cr.setCustomerSalePhone(returnCustomerBean.getPsCustomerSalePhone());
                        cr.setCustomerSaleState(returnCustomerBean.getPsCustomerSaleState());
                        cr.setCustomerSaleContractFile(returnCustomerBean.getPsCustomerSaleContractFile());
                        cr.setCustomerTaxpayerNumber(returnCustomerBean.getPsCustomerTaxpayerNumber());
                        cr.setCustomerTaxpayerAddress(returnCustomerBean.getPsCustomerTaxpayerAddress());
                        cr.setCustomerTaxpayerPhone(returnCustomerBean.getPsCustomerTaxpayerPhone());
                        cr.setCustomerTaxpayerBankName(returnCustomerBean.getPsCustomerTaxpayerBankName());
                        cr.setCustomerTaxpayerBankCode(returnCustomerBean.getPsCustomerTaxpayerBankCode());
                        cr.setCustomerLoginState(returnCustomerBean.getPsCustomerLoginState());
                        resp.setCustomerResp(cr);
                    }
                }
                // 电工信息
                if(!StringUtils.isEmpty(returnWorkOrderBean.getPsElectricianPhone())){
                    ElectricianBean electricianBean = new ElectricianBean();
                    electricianBean.setPsElectricianPhone(returnWorkOrderBean.getPsElectricianPhone());

                    ElectricianBean returnElectricianBean = electricianService.findOneByPhone(electricianBean);
                    if (null != returnElectricianBean) {
                        ElectricianResp er = new ElectricianResp();
                        er.setElectricianCode(returnElectricianBean.getPsElectricianCode());
                        er.setElectricianHead(returnElectricianBean.getPsElectricianHead());
                        er.setElectricianName(returnElectricianBean.getPsElectricianName());
                        er.setElectricianPhone(returnElectricianBean.getPsElectricianPhone());
                        er.setElectricianPwd(returnElectricianBean.getPsElectricianPwd());
                        er.setElectricianIdFrontImg(returnElectricianBean.getPsElectricianIdFrontImg());
                        er.setElectricianIdBackImg(returnElectricianBean.getPsElectricianIdBackImg());
                        er.setElectricianIdCode(returnElectricianBean.getPsElectricianIdCode());
                        er.setElectricianProvince(returnElectricianBean.getPsElectricianProvince());
                        er.setElectricianCity(returnElectricianBean.getPsElectricianCity());
                        er.setElectricianRegion(returnElectricianBean.getPsElectricianRegion());
                        er.setElectricianAddress(returnElectricianBean.getPsElectricianAddress());
                        er.setElectricianLatitude(returnElectricianBean.getPsElectricianLatitude());
                        er.setElectricianLoginState(returnElectricianBean.getPsElectricianLoginState());
                        er.setElectricianState(returnElectricianBean.getPsElectricianState());
                        er.setElectricianStateRemarks(returnElectricianBean.getPsElectricianStateRemarks());
                        resp.setElectricianResp(er);
                    }
                }
                // 评价
                List<EvaluateResp> evrs = new ArrayList<>();
                EvaluateBean evaluateBean = new EvaluateBean();
                evaluateBean.setPsWorkOrderCode(returnWorkOrderBean.getPsWorkOrderCode());
                for(EvaluateBean returnBean: evaluateService.findAllByWorkOrderCode(evaluateBean)){
                    EvaluateResp evr = new EvaluateResp();
                    evr.setFeedbackCode(returnBean.getPsFeedbackCode());
                    evr.setWorkOrderCode(returnBean.getPsWorkOrderCode());
                    evr.setEvaluateCustomerCode(returnBean.getPsEvaluateCustomerCode());
                    evr.setEvaluateCustomerUsername(returnBean.getPsEvaluateCustomerUsername());
                    evr.setEvaluateCustomerName(returnBean.getPsEvaluateCustomerName());
                    evr.setEvaluateAdminCode(returnBean.getPsEvaluateAdminCode());
                    evr.setEvaluateAdminUsername(returnBean.getPsEvaluateAdminUsername());
                    evr.setElectricianCode(returnBean.getPsElectricianCode());
                    evr.setElectricianName(returnBean.getPsElectricianName());
                    evr.setEvaluateCustomerScore(returnBean.getPsEvaluateCustomerScore());
                    evr.setEvaluateCustomerContent(returnBean.getPsEvaluateCustomerContent());
                    evr.setEvaluateAdminScore(returnBean.getPsEvaluateAdminScore());
                    evr.setEvaluateAdminContent(returnBean.getPsEvaluateAdminContent());
                    evr.setCreateDate(returnBean.getCreateDate());
                    evrs.add(evr);
                }
                // 该工单是否已查看
                resp.setWorkGlance(returnWorkOrderBean.getPsWorkGlance());

                resp.setEvaluateResps(evrs);

                Map map = new HashMap();
                map.put("data", resp);
                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "获取工单详情成功", map);
            }else {
                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "暂无数据", new HashMap());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 查询（分页）工单-支持工单状态、上报类型、工单号、客户（单位）名称、区域、客户（单位）编号、电工编号查询-条件字段可空（分页相关除外）
     * 服务器访问路径：http://localhost:8080/bolg/workOrder/findAllByCondition
     * 本地访问路径：http://localhost:8111/workOrder/findAllByCondition
     * 参数（如果不区分状态查询所有时，workOrderState为-1）：
     * {"workOrderState":1,"searchKey":"81240","feedbackType":2,"customerCode":"CUS20241102102456","customerName":"监督","feedbackRegion":"九龙坡","electricianCode":"ELE20241101212605","pageNum":"1","pageSize":"5"}
     */
    @ApiOperation(value = "查询（分页）工单-支持工单状态、上报类型、工单号、客户（单位）名称、区域、客户（单位）编号、电工编号查询-条件字段可空（分页相关除外）", notes = "参数（如果不区分状态查询所有时，workOrderState为-1）：{\"workOrderState\":1,\"searchKey\":\"81240\",\"feedbackType\":2,\"customerCode\":\"CUS20241102102456\",\"customerName\":\"监督\",\"feedbackRegion\":\"九龙坡\",\"electricianCode\":\"ELE20241101212605\",\"pageNum\":\"1\",\"pageSize\":\"5\"}", response = WorkOrderResp.class)
    @RequestMapping(value = "/findAllByCondition",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String findAllByCondition(@RequestBody WorkOrderRequest request){
        Page pageInfo = PageHelper.startPage(request.getPageNum(), request.getPageSize());
        String result;

        WorkOrderBean workOrderBean = new WorkOrderBean();
        workOrderBean.setPsWorkOrderState(request.getWorkOrderState());
        if(!StringUtils.isEmpty(request.getSearchKey())){
            if(request.getSearchKey().matches("\\d+")){
                workOrderBean.setPsWorkOrderCode(request.getSearchKey());
            }else{
                workOrderBean.setPsCustomerName(request.getSearchKey());
            }
        }
        workOrderBean.setPsCustomerName(request.getCustomerName());
        workOrderBean.setPsFeedbackRegion(request.getFeedbackRegion());
        workOrderBean.setPsCustomerCode(request.getCustomerCode());
        workOrderBean.setPsElectricianCode(request.getElectricianCode());
        workOrderBean.setPsFeedbackType(request.getFeedbackType());

        try {
            List<WorkOrderResp> respList = new ArrayList<>();
            for(WorkOrderBean returnBean : service.findAllByCondition(workOrderBean)){
                WorkOrderResp resp = new WorkOrderResp();
                resp.setWorkOrderCode(returnBean.getPsWorkOrderCode());
                resp.setFeedbackCode(returnBean.getPsFeedbackCode());
                resp.setCustomerCode(returnBean.getPsCustomerCode());
                resp.setCustomerName(returnBean.getPsCustomerName());
                resp.setCustomerUserName(returnBean.getPsCustomerUserName());
                resp.setCustomerUserPhone(returnBean.getPsCustomerUserPhone());
                resp.setElectricianCode(returnBean.getPsElectricianCode());
                resp.setElectricianName(returnBean.getPsElectricianName());
                resp.setElectricianPhone(returnBean.getPsElectricianPhone());
                resp.setAdminUserCode(returnBean.getPsAdminUserCode());
                resp.setWorkOrderContent(returnBean.getPsWorkOrderContent());
                resp.setWorkOrderVoiceFile(returnBean.getPsWorkOrderVoiceFile());
                resp.setWorkOrderVideoFile(returnBean.getPsWorkOrderVideoFile());
                resp.setWorkOrderImgFile(returnBean.getPsWorkOrderImgFile());
                resp.setFeedbackDate(returnBean.getPsFeedbackDate());
                resp.setFeedbackProvince(returnBean.getPsFeedbackProvince());
                resp.setFeedbackCity(returnBean.getPsFeedbackCity());
                resp.setFeedbackRegion(returnBean.getPsFeedbackRegion());
                resp.setFeedbackAddress(returnBean.getPsFeedbackAddress());
                resp.setFeedbackLongitude(returnBean.getPsFeedbackLongitude());
                resp.setFeedbackLatitude(returnBean.getPsFeedbackLatitude());
                resp.setFeedbackType(returnBean.getPsFeedbackType());
                resp.setWorkOrderType(returnBean.getPsWorkOrderType());
                resp.setWorkOrderEffectDate(returnBean.getPsWorkOrderEffectDate());
                resp.setWorkOrderRequirementDate(returnBean.getPsWorkOrderRequirementDate());
                resp.setWorkOrderAcceptDate(returnBean.getPsWorkOrderAcceptDate());
                resp.setWorkOrderCreateRemarks(returnBean.getPsWorkOrderCreateRemarks());
                resp.setWorkOrderArriveProvince(returnBean.getPsWorkOrderArriveProvince());
                resp.setWorkOrderArriveCity(returnBean.getPsWorkOrderArriveCity());
                resp.setWorkOrderArriveRegion(returnBean.getPsWorkOrderArriveRegion());
                resp.setWorkOrderArriveAddress(returnBean.getPsWorkOrderArriveAddress());
                resp.setWorkOrderArriveLongitude(returnBean.getPsWorkOrderArriveLongitude());
                resp.setWorkOrderArriveLatitude(returnBean.getPsWorkOrderArriveLatitude());
                resp.setWorkOrderArriveImg(returnBean.getPsWorkOrderArriveImg());
                resp.setWorkOrderArriveDate(returnBean.getPsWorkOrderArriveDate());
                resp.setWorkOrderTestingState(returnBean.getPsWorkOrderTestingState());
                resp.setWorkOrderRepairState(returnBean.getPsWorkOrderRepairState());
                resp.setTestingCode(returnBean.getPsTestingCode());
                resp.setRepairCode(returnBean.getPsRepairCode());
                resp.setWorkOrderState(returnBean.getPsWorkOrderState());

                EvaluateBean evaluateBean = new EvaluateBean();
                evaluateBean.setPsWorkOrderCode(returnBean.getPsWorkOrderCode());
                List<EvaluateBean> returnEvaluateBeans = evaluateService.findAllByWorkOrderCode(evaluateBean);
                if(null != returnEvaluateBeans && returnEvaluateBeans.size() != 0){
                    resp.setIsEvaluate(1);
                }else{
                    resp.setIsEvaluate(0);
                }
                resp.setWorkGlance(returnBean.getPsWorkGlance());

                respList.add(resp);
            }

            Map map = new HashMap();
            map.put("pageInfo", pageInfo);
            map.put("rows", respList);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 查询（分页）工单-支持工单状态、上报类型、工单号、客户（单位）名称、区域、客户（单位）编号、电工编号查询-条件字段可空（分页相关除外）
     * 服务器访问路径：http://localhost:8080/bolg/workOrder/findAllByConditionToMe
     * 本地访问路径：http://localhost:8111/workOrder/findAllByConditionToMe
     * 参数（如果不区分状态查询所有时，workOrderState为-1）：
     * {"workOrderState":1,"searchKey":"81240","feedbackType":2,"customerCode":"CUS20241102102456","customerName":"监督","feedbackRegion":"九龙坡","electricianCode":"ELE20241101212605","pageNum":"1","pageSize":"5"}
     * 根据电工编号查询不同状态的订单参数：
     * {"workOrderState":3,"searchKey":"","feedbackType":3,"customerCode":"","customerName":"","feedbackRegion":"","electricianCode":"ELE20241218235907","pageNum":"1","pageSize":"5"}
     */
    @ApiOperation(value = "查询（分页）工单-电工端我的工单用-支持工单状态、上报类型、工单号、客户（单位）名称、区域、客户（单位）编号、电工编号查询-条件字段可空（分页相关除外）", notes = "参数（如果不区分状态查询所有时，workOrderState为-1）：{\"workOrderState\":1,\"searchKey\":\"81240\",\"feedbackType\":2,\"customerCode\":\"CUS20241102102456\",\"customerName\":\"监督\",\"feedbackRegion\":\"九龙坡\",\"electricianCode\":\"ELE20241101212605\",\"pageNum\":\"1\",\"pageSize\":\"5\"}", response = WorkOrderResp.class)
    @RequestMapping(value = "/findAllByConditionToMe",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String findAllByConditionToMe(@RequestBody WorkOrderRequest request){
        Page pageInfo = PageHelper.startPage(request.getPageNum(), request.getPageSize());
        String result;

        WorkOrderBean workOrderBean = new WorkOrderBean();
        workOrderBean.setPsWorkOrderState(request.getWorkOrderState());
        if(!StringUtils.isEmpty(request.getSearchKey())){
            if(request.getSearchKey().matches("\\d+")){
                workOrderBean.setPsWorkOrderCode(request.getSearchKey());
            }else{
                workOrderBean.setPsCustomerName(request.getSearchKey());
            }
        }
        workOrderBean.setPsCustomerName(request.getCustomerName());
        workOrderBean.setPsFeedbackRegion(request.getFeedbackRegion());
        workOrderBean.setPsCustomerCode(request.getCustomerCode());
        workOrderBean.setPsElectricianCode(request.getElectricianCode());
        workOrderBean.setPsFeedbackType(request.getFeedbackType());

        try {
            List<WorkOrderResp> respList = new ArrayList<>();
            for(WorkOrderBean returnBean : service.findAllByCondition(workOrderBean)){
                WorkOrderResp resp = new WorkOrderResp();
                resp.setWorkOrderCode(returnBean.getPsWorkOrderCode());
                resp.setFeedbackCode(returnBean.getPsFeedbackCode());
                resp.setCustomerCode(returnBean.getPsCustomerCode());
                resp.setCustomerName(returnBean.getPsCustomerName());
                resp.setCustomerUserName(returnBean.getPsCustomerUserName());
                resp.setCustomerUserPhone(returnBean.getPsCustomerUserPhone());
                resp.setElectricianCode(returnBean.getPsElectricianCode());
                resp.setElectricianName(returnBean.getPsElectricianName());
                resp.setElectricianPhone(returnBean.getPsElectricianPhone());
                resp.setAdminUserCode(returnBean.getPsAdminUserCode());
                resp.setWorkOrderContent(returnBean.getPsWorkOrderContent());
                resp.setWorkOrderVoiceFile(returnBean.getPsWorkOrderVoiceFile());
                resp.setWorkOrderVideoFile(returnBean.getPsWorkOrderVideoFile());
                resp.setWorkOrderImgFile(returnBean.getPsWorkOrderImgFile());
                resp.setFeedbackDate(returnBean.getPsFeedbackDate());
                resp.setFeedbackProvince(returnBean.getPsFeedbackProvince());
                resp.setFeedbackCity(returnBean.getPsFeedbackCity());
                resp.setFeedbackRegion(returnBean.getPsFeedbackRegion());
                resp.setFeedbackAddress(returnBean.getPsFeedbackAddress());
                resp.setFeedbackLongitude(returnBean.getPsFeedbackLongitude());
                resp.setFeedbackLatitude(returnBean.getPsFeedbackLatitude());
                resp.setFeedbackType(returnBean.getPsFeedbackType());
                resp.setWorkOrderType(returnBean.getPsWorkOrderType());
                resp.setWorkOrderEffectDate(returnBean.getPsWorkOrderEffectDate());
                resp.setWorkOrderRequirementDate(returnBean.getPsWorkOrderRequirementDate());
                resp.setWorkOrderAcceptDate(returnBean.getPsWorkOrderAcceptDate());
                resp.setWorkOrderCreateRemarks(returnBean.getPsWorkOrderCreateRemarks());
                resp.setWorkOrderArriveProvince(returnBean.getPsWorkOrderArriveProvince());
                resp.setWorkOrderArriveCity(returnBean.getPsWorkOrderArriveCity());
                resp.setWorkOrderArriveRegion(returnBean.getPsWorkOrderArriveRegion());
                resp.setWorkOrderArriveAddress(returnBean.getPsWorkOrderArriveAddress());
                resp.setWorkOrderArriveLongitude(returnBean.getPsWorkOrderArriveLongitude());
                resp.setWorkOrderArriveLatitude(returnBean.getPsWorkOrderArriveLatitude());
                resp.setWorkOrderArriveImg(returnBean.getPsWorkOrderArriveImg());
                resp.setWorkOrderArriveDate(returnBean.getPsWorkOrderArriveDate());
                resp.setWorkOrderTestingState(returnBean.getPsWorkOrderTestingState());
                resp.setWorkOrderRepairState(returnBean.getPsWorkOrderRepairState());
                resp.setTestingCode(returnBean.getPsTestingCode());
                resp.setRepairCode(returnBean.getPsRepairCode());
                resp.setWorkOrderState(returnBean.getPsWorkOrderState());

                EvaluateBean evaluateBean = new EvaluateBean();
                evaluateBean.setPsWorkOrderCode(returnBean.getPsWorkOrderCode());
                List<EvaluateBean> returnEvaluateBeans = evaluateService.findAllByWorkOrderCode(evaluateBean);
                if(null != returnEvaluateBeans && returnEvaluateBeans.size() != 0){
                    resp.setIsEvaluate(1);
                }else{
                    resp.setIsEvaluate(0);
                }
                resp.setWorkGlance(returnBean.getPsWorkGlance());

                respList.add(resp);
            }

            Iterator<WorkOrderResp> woRespIt = respList.iterator();
            while(woRespIt.hasNext()){
                WorkOrderResp woResp = woRespIt.next();
                if (woResp.getWorkOrderState() == 0 || woResp.getWorkOrderState() == 1) {
                    woRespIt.remove();
                }
            }

            for(WorkOrderResp soResp: respList){
                if(!StringUtils.isEmpty(soResp.getTestingCode())){
                    TestingBean testingBean = new TestingBean();
                    testingBean.setPsTestingCode(soResp.getTestingCode());
                    TestingBean returnTestingBean = testingService.findOneByCode(testingBean);
                    if(null != returnTestingBean){
                        soResp.setIsTestingAgree(returnTestingBean.getPsTestingAgree());
                        soResp.setTestingTotal(returnTestingBean.getPsTestingTotal());
                    }else{
                        soResp.setTestingTotal(-1);
                    }
                }
            }

            Map map = new HashMap();
            map.put("pageInfo", pageInfo);
            map.put("rows", respList);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 电工端小程序首页查询（分页）工单-条件字段可空（分页相关除外）
     * 服务器访问路径：http://localhost:8080/bolg/workOrder/findAllToIndex
     * 本地访问路径：http://localhost:8111/workOrder/findAllToIndex
     * 查询派遣给我的参数：
     *  {
     *     "electricianCode": "ELE20241218235907",
     *     "feedbackRegion": "",
     *     "workOrderType": 1,
     *     "pageNum": "1",
     *     "pageSize": "5"
     * }
     * 查询可以抢单的参数：
     * {
     *     "electricianCode": "",
     *     "feedbackRegion": "",
     *     "workOrderType": 2,
     *     "pageNum": "1",
     *     "pageSize": "5"
     * }
     */
    @ApiOperation(value = "电工端小程序首页查询（分页）工单-条件字段可空（电工编号及分页相关除外）", notes = "参数：联系后端开发", response = WorkOrderResp.class)
    @RequestMapping(value = "/findAllToIndex",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String findAllToIndex(@RequestBody WorkOrderRequest request){
        Page pageInfo = PageHelper.startPage(request.getPageNum(), request.getPageSize());
        String result;

        WorkOrderBean workOrderBean = new WorkOrderBean();
        workOrderBean.setPsElectricianCode(request.getElectricianCode());
        workOrderBean.setPsFeedbackRegion(request.getFeedbackRegion());
        workOrderBean.setPsWorkOrderRequirementDate(request.getWorkOrderRequirementDate());
        workOrderBean.setPsWorkOrderType(request.getWorkOrderType());

        try {
            List<WorkOrderResp> respList = new ArrayList<>();
            for(WorkOrderBean returnBean : service.findAllToIndex(workOrderBean)){
                WorkOrderResp resp = new WorkOrderResp();
                resp.setWorkOrderCode(returnBean.getPsWorkOrderCode());
                resp.setFeedbackCode(returnBean.getPsFeedbackCode());
                resp.setCustomerCode(returnBean.getPsCustomerCode());
                resp.setCustomerName(returnBean.getPsCustomerName());
                resp.setCustomerUserName(returnBean.getPsCustomerUserName());
                resp.setCustomerUserPhone(returnBean.getPsCustomerUserPhone());
                resp.setElectricianCode(returnBean.getPsElectricianCode());
                resp.setElectricianName(returnBean.getPsElectricianName());
                resp.setElectricianPhone(returnBean.getPsElectricianPhone());
                resp.setAdminUserCode(returnBean.getPsAdminUserCode());
                resp.setWorkOrderContent(returnBean.getPsWorkOrderContent());
                resp.setWorkOrderVoiceFile(returnBean.getPsWorkOrderVoiceFile());
                resp.setWorkOrderVideoFile(returnBean.getPsWorkOrderVideoFile());
                resp.setWorkOrderImgFile(returnBean.getPsWorkOrderImgFile());
                resp.setFeedbackDate(returnBean.getPsFeedbackDate());
                resp.setFeedbackProvince(returnBean.getPsFeedbackProvince());
                resp.setFeedbackCity(returnBean.getPsFeedbackCity());
                resp.setFeedbackRegion(returnBean.getPsFeedbackRegion());
                resp.setFeedbackAddress(returnBean.getPsFeedbackAddress());
                resp.setFeedbackLongitude(returnBean.getPsFeedbackLongitude());
                resp.setFeedbackLatitude(returnBean.getPsFeedbackLatitude());
                resp.setFeedbackType(returnBean.getPsFeedbackType());
                resp.setWorkOrderType(returnBean.getPsWorkOrderType());
                resp.setWorkOrderEffectDate(returnBean.getPsWorkOrderEffectDate());
                resp.setWorkOrderRequirementDate(returnBean.getPsWorkOrderRequirementDate());
                resp.setWorkOrderAcceptDate(returnBean.getPsWorkOrderAcceptDate());
                resp.setWorkOrderCreateRemarks(returnBean.getPsWorkOrderCreateRemarks());
                resp.setWorkOrderArriveProvince(returnBean.getPsWorkOrderArriveProvince());
                resp.setWorkOrderArriveCity(returnBean.getPsWorkOrderArriveCity());
                resp.setWorkOrderArriveRegion(returnBean.getPsWorkOrderArriveRegion());
                resp.setWorkOrderArriveAddress(returnBean.getPsWorkOrderArriveAddress());
                resp.setWorkOrderArriveLongitude(returnBean.getPsWorkOrderArriveLongitude());
                resp.setWorkOrderArriveLatitude(returnBean.getPsWorkOrderArriveLatitude());
                resp.setWorkOrderArriveImg(returnBean.getPsWorkOrderArriveImg());
                resp.setWorkOrderArriveDate(returnBean.getPsWorkOrderArriveDate());
                resp.setWorkOrderTestingState(returnBean.getPsWorkOrderTestingState());
                resp.setWorkOrderRepairState(returnBean.getPsWorkOrderRepairState());
                resp.setTestingCode(returnBean.getPsTestingCode());
                resp.setRepairCode(returnBean.getPsRepairCode());
                resp.setWorkOrderState(returnBean.getPsWorkOrderState());

                EvaluateBean evaluateBean = new EvaluateBean();
                evaluateBean.setPsWorkOrderCode(returnBean.getPsWorkOrderCode());
                List<EvaluateBean> returnEvaluateBeans = evaluateService.findAllByWorkOrderCode(evaluateBean);
                if(null != returnEvaluateBeans && returnEvaluateBeans.size() != 0){
                    resp.setIsEvaluate(1);
                }else{
                    resp.setIsEvaluate(0);
                }
                resp.setWorkGlance(returnBean.getPsWorkGlance());

                respList.add(resp);
            }

            Map map = new HashMap();
            map.put("pageInfo", pageInfo);
            map.put("rows", respList);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 派遣工单
     * 服务器访问路径：http://localhost:8080/bolg/workOrder/dispatch
     * 本地访问路径：http://localhost:8111/workOrder/dispatch
     * 参数：
     * {"workOrderCode":"WO20241111181421","electricianCode":"ELE20241101212605","electricianName":"郭富城","electricianPhone":"15123390992","workOrderRequirementDate":8}
     */
    @ApiOperation(value = "派遣工单", notes = "参数：{\"workOrderCode\":\"WO20241111181421\",\"electricianCode\":\"ELE20241101212605\",\"electricianName\":\"郭富城\",\"electricianPhone\":\"15123390992\",\"workOrderRequirementDate\":8}")
    @RequestMapping(value = "/dispatch",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String dispatch(@RequestBody WorkOrderRequest request){
        String result;

        WorkOrderBean workOrderBean = new WorkOrderBean();
        workOrderBean.setPsWorkOrderCode(request.getWorkOrderCode());
        workOrderBean.setPsElectricianCode(request.getElectricianCode());
        workOrderBean.setPsElectricianName(request.getElectricianName());
        workOrderBean.setPsElectricianPhone(request.getElectricianPhone());
        workOrderBean.setPsWorkOrderType(1);
        workOrderBean.setPsWorkOrderEffectDate(DateUtil.getSystemDate());
        workOrderBean.setPsWorkOrderRequirementDate(request.getWorkOrderRequirementDate());
        workOrderBean.setPsWorkOrderState(1);
        workOrderBean.setPsWorkGlance(0);

        try {
            service.dispatch(workOrderBean);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "派遣工单成功", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 发布工单
     * 服务器访问路径：http://localhost:8080/bolg/workOrder/publish
     * 本地访问路径：http://localhost:8111/workOrder/publish
     * 参数：
     * {"workOrderCode":"WO20241111181421", "workOrderRequirementDate":8}
     */
    @ApiOperation(value = "发布工单", notes = "参数：{\"workOrderCode\":\"WO20241111181421\", \"workOrderRequirementDate\":8}")
    @RequestMapping(value = "/publish",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String publish(@RequestBody WorkOrderRequest request){
        String result;

        WorkOrderBean workOrderBean = new WorkOrderBean();
        workOrderBean.setPsWorkOrderCode(request.getWorkOrderCode());
        workOrderBean.setPsWorkOrderType(2);
        workOrderBean.setPsWorkOrderEffectDate(DateUtil.getSystemDate());
        workOrderBean.setPsWorkOrderRequirementDate(request.getWorkOrderRequirementDate());
        workOrderBean.setPsWorkOrderState(1);

        try {
            service.publish(workOrderBean);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "发布工单成功", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 到场打卡
     * 服务器访问路径：http://localhost:8080/bolg/workOrder/arrive
     * 本地访问路径：http://localhost:8111/workOrder/arrive
     * 参数：
     * {"workOrderCode":"WROR20241111222936","electricianCode":"ELE20241101212605","workOrderArriveProvince":"重庆市","workOrderArriveCity":"重庆市","workOrderArriveRegion":"渝北区","workOrderArriveAddress":"黄山大道3号","workOrderArriveLongitude":32.92332,"workOrderArriveLatitude":130.12312,"workOrderArriveImg":"https://ww1.sinaimg.cn/mw690/005x3T92ly1hrfa3mkow1j31kw1kw49y.jpg"}
     */
    @ApiOperation(value = "到场打卡", notes = "参数：{\"workOrderCode\":\"WROR20241111222936\",\"electricianCode\":\"ELE20241101212605\",\"workOrderArriveProvince\":\"重庆市\",\"workOrderArriveCity\":\"重庆市\",\"workOrderArriveRegion\":\"渝北区\",\"workOrderArriveAddress\":\"黄山大道3号\",\"workOrderArriveLongitude\":32.92332,\"workOrderArriveLatitude\":130.12312,\"workOrderArriveImg\":\"https://ww1.sinaimg.cn/mw690/005x3T92ly1hrfa3mkow1j31kw1kw49y.jpg\"}")
    @RequestMapping(value = "/arrive",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String arrive(@RequestBody WorkOrderRequest request){
        String result;

        WorkOrderBean workOrderBean = new WorkOrderBean();
        workOrderBean.setPsWorkOrderCode(request.getWorkOrderCode());
        workOrderBean.setPsElectricianCode(request.getElectricianCode());
        workOrderBean.setPsWorkOrderArriveProvince(request.getWorkOrderArriveProvince());
        workOrderBean.setPsWorkOrderArriveCity(request.getWorkOrderArriveCity());
        workOrderBean.setPsWorkOrderArriveRegion(request.getWorkOrderArriveRegion());
        workOrderBean.setPsWorkOrderArriveAddress(request.getWorkOrderArriveAddress());
        workOrderBean.setPsWorkOrderArriveLongitude(request.getWorkOrderArriveLongitude());
        workOrderBean.setPsWorkOrderArriveLatitude(request.getWorkOrderArriveLatitude());
        workOrderBean.setPsWorkOrderArriveImg(request.getWorkOrderArriveImg());
        workOrderBean.setPsWorkOrderArriveDate(DateUtil.getSystemDate());
        workOrderBean.setPsWorkOrderState(3);

        try {
            service.arrive(workOrderBean);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "到场打卡成功", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 接单
     * 服务器访问路径：http://localhost:8080/bolg/workOrder/accept
     * 本地访问路径：http://localhost:8111/workOrder/accept
     * 参数：
     * {"workOrderCode":"WROR20241111181421","electricianCode":"ELE20241101212605"}
     */
    @ApiOperation(value = "接单", notes = "参数：{\"workOrderCode\":\"WROR20241111181421\",\"electricianCode\":\"ELE20241101212605\"}")
    @RequestMapping(value = "/accept",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String accept(@RequestBody WorkOrderRequest request){
        String result;

        try {
            ElectricianBean electricianBean = new ElectricianBean();
            electricianBean.setPsElectricianCode(request.getElectricianCode());
            ElectricianBean returnElectricianBean = electricianService.findOneByCode(electricianBean);
            if(returnElectricianBean.getPsElectricianState() == 2){
                WorkOrderBean workOrderBean = new WorkOrderBean();
                workOrderBean.setPsWorkOrderCode(request.getWorkOrderCode());
                workOrderBean.setPsElectricianCode(request.getElectricianCode());
                workOrderBean.setPsWorkOrderAcceptDate(DateUtil.getSystemDate());
                workOrderBean.setPsWorkOrderState(2);
                service.accept(workOrderBean);
                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "接单成功", new HashMap());
            }else{
                if(returnElectricianBean.getPsElectricianState() == 1){
                    result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, "审核中", new HashMap());
                }else if(returnElectricianBean.getPsElectricianState() == 3){
                    result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, "审核未通过", new HashMap());
                }else if(returnElectricianBean.getPsElectricianState() == 4){
                    result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, "账号被封禁", new HashMap());
                }else{
                    result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, "未知错误", new HashMap());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 抢单
     * 服务器访问路径：http://localhost:8080/bolg/workOrder/grabOrder
     * 本地访问路径：http://localhost:8111/workOrder/grabOrder
     * 参数：
     * {"workOrderCode":"WROR20241111181421","electricianCode":"ELE20241101212605","electricianName":"郭富城","electricianPhone":"15123390992"}
     */
    @ApiOperation(value = "抢单", notes = "参数：{\"workOrderCode\":\"WROR20241111181421\",\"electricianCode\":\"ELE20241101212605\",\"electricianName\":\"郭富城\",\"electricianPhone\":\"15123390992\"}")
    @RequestMapping(value = "/grabOrder",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String grabOrder(@RequestBody WorkOrderRequest request){
        String result;

        try {
            ElectricianBean electricianBean = new ElectricianBean();
            electricianBean.setPsElectricianCode(request.getElectricianCode());
            ElectricianBean returnElectricianBean = electricianService.findOneByCode(electricianBean);
            if(returnElectricianBean.getPsElectricianState() == 2){
                WorkOrderBean workOrderBean = new WorkOrderBean();
                workOrderBean.setPsWorkOrderCode(request.getWorkOrderCode());
                workOrderBean.setPsElectricianCode(request.getElectricianCode());
                workOrderBean.setPsElectricianName(request.getElectricianName());
                workOrderBean.setPsElectricianPhone(request.getElectricianPhone());
                workOrderBean.setPsWorkOrderAcceptDate(DateUtil.getSystemDate());
                workOrderBean.setPsWorkOrderState(2);
                service.grabOrder(workOrderBean);
                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "抢单成功", new HashMap());
            }else{
                if(returnElectricianBean.getPsElectricianState() == 1){
                    result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, "审核中", new HashMap());
                }else if(returnElectricianBean.getPsElectricianState() == 3){
                    result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, "审核未通过", new HashMap());
                }else if(returnElectricianBean.getPsElectricianState() == 4){
                    result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, "账号被封禁", new HashMap());
                }else{
                    result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, "未知错误", new HashMap());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 拒绝接单
     * 服务器访问路径：http://localhost:8080/bolg/workOrder/refuse
     * 本地访问路径：http://localhost:8111/workOrder/refuse
     * 参数：
     * {"workOrderCode":"WROR20241111181421","electricianCode":"ELE20241101212605"}
     */
    @ApiOperation(value = "拒绝接单", notes = "参数：{\"workOrderCode\":\"WROR20241111181421\",\"electricianCode\":\"ELE20241101212605\"}")
    @RequestMapping(value = "/refuse",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String refuse(@RequestBody WorkOrderRequest request){
        String result;

        try {
            ElectricianBean electricianBean = new ElectricianBean();
            electricianBean.setPsElectricianCode(request.getElectricianCode());
            ElectricianBean returnElectricianBean = electricianService.findOneByCode(electricianBean);
            if(returnElectricianBean.getPsElectricianState() == 2){
                WorkOrderBean workOrderBean = new WorkOrderBean();
                workOrderBean.setPsWorkOrderCode(request.getWorkOrderCode());
                workOrderBean.setPsElectricianCode(request.getElectricianCode());
                workOrderBean.setPsWorkOrderAcceptDate(DateUtil.getSystemDate());
                workOrderBean.setPsWorkOrderState(0);
                service.refuse(workOrderBean);
                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "拒绝接单成功", new HashMap());
            }else{
                if(returnElectricianBean.getPsElectricianState() == 1){
                    result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, "审核中", new HashMap());
                }else if(returnElectricianBean.getPsElectricianState() == 3){
                    result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, "审核未通过", new HashMap());
                }else if(returnElectricianBean.getPsElectricianState() == 4){
                    result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, "账号被封禁", new HashMap());
                }else{
                    result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, "未知错误", new HashMap());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 关单
     * 服务器访问路径：http://localhost:8080/bolg/workOrder/endWorkOrder
     * 本地访问路径：http://localhost:8111/workOrder/endWorkOrder
     * 参数：
     * {"workOrderCode":"WROR20241111181421"}
     */
    @ApiOperation(value = "关单", notes = "参数：{\"workOrderCode\":\"WROR20241111181421\"}")
    @RequestMapping(value = "/endWorkOrder",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String endWorkOrder(@RequestBody WorkOrderRequest request){
        String result;

        WorkOrderBean workOrderBean = new WorkOrderBean();
        workOrderBean.setPsWorkOrderCode(request.getWorkOrderCode());
        workOrderBean.setPsWorkOrderState(6);

        try {
            service.endWorkOrder(workOrderBean);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "关单成功", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 重新生成工单
     * 服务器访问路径：http://localhost:8080/bolg/workOrder/doItAgainWorkOrder
     * 本地访问路径：http://localhost:8111/workOrder/doItAgainWorkOrder
     * 参数：
     * {"workOrderCode":"WROR20241111181421"}
     */
    @ApiOperation(value = "重新生成工单", notes = "参数：{\"workOrderCode\":\"WROR20241111181421\"}")
    @RequestMapping(value = "/doItAgainWorkOrder",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String doItAgainWorkOrder(@RequestBody WorkOrderRequest request){
        String result;

        WorkOrderBean workOrderBean = new WorkOrderBean();
        workOrderBean.setPsWorkOrderCode(request.getWorkOrderCode());
        workOrderBean.setPsWorkOrderState(6);

        try {
            // 将工单状态改为已结束
            service.doItAgainWorkOrder(workOrderBean);

            // 获取当前工单信息重新生成工单
            WorkOrderBean returnBean = service.findOneByCode(workOrderBean);

            WorkOrderBean workOrderDoItAgainBean = new WorkOrderBean();
            workOrderDoItAgainBean.setPsWorkOrderCode(CodeGenerateUtil.getWorkOrderCode());
            workOrderDoItAgainBean.setPsFeedbackCode(returnBean.getPsFeedbackCode());
            workOrderDoItAgainBean.setPsCustomerCode(returnBean.getPsCustomerCode());
            workOrderDoItAgainBean.setPsCustomerName(returnBean.getPsCustomerName());
            workOrderDoItAgainBean.setPsCustomerUserName(returnBean.getPsCustomerUserName());
            workOrderDoItAgainBean.setPsCustomerUserPhone(returnBean.getPsCustomerUserPhone());
            workOrderDoItAgainBean.setPsAdminUserCode(returnBean.getPsAdminUserCode());
            workOrderDoItAgainBean.setPsWorkOrderContent(returnBean.getPsWorkOrderContent());
            workOrderDoItAgainBean.setPsWorkOrderImgFile(returnBean.getPsWorkOrderImgFile());
            workOrderDoItAgainBean.setPsFeedbackDate(returnBean.getPsFeedbackDate());
            workOrderDoItAgainBean.setPsFeedbackProvince(returnBean.getPsFeedbackProvince());
            workOrderDoItAgainBean.setPsFeedbackCity(returnBean.getPsFeedbackCity());
            workOrderDoItAgainBean.setPsFeedbackRegion(returnBean.getPsFeedbackRegion());
            workOrderDoItAgainBean.setPsFeedbackAddress(returnBean.getPsFeedbackAddress());
            workOrderDoItAgainBean.setPsFeedbackLongitude(returnBean.getPsFeedbackLongitude());
            workOrderDoItAgainBean.setPsFeedbackLatitude(returnBean.getPsFeedbackLatitude());
            workOrderDoItAgainBean.setPsFeedbackType(returnBean.getPsFeedbackType());
            workOrderDoItAgainBean.setPsWorkOrderTestingState(2);
            workOrderDoItAgainBean.setPsWorkOrderRepairState(2);
            workOrderDoItAgainBean.setPsWorkOrderType(returnBean.getPsWorkOrderType());
            workOrderDoItAgainBean.setPsWorkOrderCreateRemarks(returnBean.getPsWorkOrderCreateRemarks());

            service.add(workOrderDoItAgainBean);

            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "重新生成工单成功", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 获取我的工单信息状态统计
     * 服务器访问路径：http://localhost:8080/bolg/workOrder/getWorkOrderStatistics
     * 本地访问路径：http://localhost:8111/workOrder/getWorkOrderStatistics
     * 参数：
     * {"customerCode":"CUS20241224151636"}
     */
    @ApiOperation(value = "获取我的工单信息状态统计", notes = "参数：{\"customerCode\":\"CUS20241224151636\"}", response = WorkOrderResp.class)
    @RequestMapping(value = "/getWorkOrderStatistics",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String getWorkOrderStatistics(@RequestBody WorkOrderRequest request){
        String result;
        try {
            WorkOrderBean workOrderBean = new WorkOrderBean();
            workOrderBean.setPsCustomerCode(request.getCustomerCode());
            workOrderBean.setPsWorkOrderState(-1);
            workOrderBean.setPsFeedbackType(3);

            List<WorkOrderBean> returnAllBeans = service.findAllByCondition(workOrderBean);

            WorkOrderStatisticsResp wosResp = new WorkOrderStatisticsResp();
            int waitNum = 0;
            int handledNum = 0;
            int receiveNum = 0;
            int reachNum = 0;
            int detectionNum = 0;
            int maintenanceNum = 0;
            int endNum = 0;

            if(null != returnAllBeans && returnAllBeans.size() != 0){
                for(WorkOrderBean wob: returnAllBeans){
                    if(wob.getPsWorkOrderState() == 0){
                        waitNum += 1;
                    } else if(wob.getPsWorkOrderState() == 1){
                        handledNum += 1;
                    } else if(wob.getPsWorkOrderState() == 2){
                        receiveNum += 1;
                    } else if(wob.getPsWorkOrderState() == 3){
                        reachNum += 1;
                    } else if(wob.getPsWorkOrderState() == 4){
                        detectionNum += 1;
                    } else if(wob.getPsWorkOrderState() == 5){
                        maintenanceNum += 1;
                    } else if(wob.getPsWorkOrderState() == 6){
                        endNum += 1;
                    }
                }
                wosResp.setWaitNum(waitNum);
                wosResp.setHandledNum(handledNum);
                wosResp.setReceiveNum(receiveNum);
                wosResp.setReachNum(reachNum);
                wosResp.setDetectionNum(detectionNum);
                wosResp.setMaintenanceNum(maintenanceNum);
                wosResp.setEndNum(endNum);
            }else{
                wosResp.setWaitNum(0);
                wosResp.setHandledNum(0);
                wosResp.setReceiveNum(0);
                wosResp.setReachNum(0);
                wosResp.setDetectionNum(0);
                wosResp.setMaintenanceNum(0);
                wosResp.setEndNum(0);
            }

            Map map = new HashMap();
            map.put("data", wosResp);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 获取我的工单信息状态统计（电工端用）
     * 服务器访问路径：http://localhost:8080/bolg/workOrder/getWorkOrderStatisticsToElectrician
     * 本地访问路径：http://localhost:8111/workOrder/getWorkOrderStatisticsToElectrician
     * 参数：
     * {"electricianCode":"ELE20241218235907"}
     */
    @ApiOperation(value = "获取我的工单信息状态统计（电工端用）", notes = "参数：{\"electricianCode\":\"ELE20241218235907\"}", response = WorkOrderResp.class)
    @RequestMapping(value = "/getWorkOrderStatisticsToElectrician",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String getWorkOrderStatisticsToElectrician(@RequestBody WorkOrderRequest request){
        String result;
        try {
            WorkOrderBean workOrderBean = new WorkOrderBean();
            workOrderBean.setPsElectricianCode(request.getElectricianCode());
            workOrderBean.setPsWorkOrderState(-1);
            workOrderBean.setPsFeedbackType(3);

            List<WorkOrderBean> returnAllBeans = service.findAllByCondition(workOrderBean);

            WorkOrderStatisticsResp wosResp = new WorkOrderStatisticsResp();
            int receiveNum = 0;
            int reachNum = 0;
            int detectionNum = 0;
            int maintenanceNum = 0;
            int endNum = 0;

            if(null != returnAllBeans && returnAllBeans.size() != 0){
                for(WorkOrderBean wob: returnAllBeans){
                    if(wob.getPsWorkOrderState() == 2){
                        receiveNum += 1;
                    } else if(wob.getPsWorkOrderState() == 3){
                        reachNum += 1;
                    } else if(wob.getPsWorkOrderState() == 4){
                        detectionNum += 1;
                    } else if(wob.getPsWorkOrderState() == 5){
                        maintenanceNum += 1;
                    } else if(wob.getPsWorkOrderState() == 6){
                        endNum += 1;
                    }
                }
                wosResp.setReceiveNum(receiveNum);
                wosResp.setReachNum(reachNum);
                wosResp.setDetectionNum(detectionNum);
                wosResp.setMaintenanceNum(maintenanceNum);
                wosResp.setEndNum(endNum);
            }else{
                wosResp.setReceiveNum(0);
                wosResp.setReachNum(0);
                wosResp.setDetectionNum(0);
                wosResp.setMaintenanceNum(0);
                wosResp.setEndNum(0);
            }

            Map map = new HashMap();
            map.put("data", wosResp);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 查询未读条数
     * 服务器访问路径：http://localhost:8080/bolg/workOrder/findUnread
     * 本地访问路径：http://localhost:8111/workOrder/findUnread
     * 参数：
     * {"electricianCode":"ELE20241224163321"}
     */
    @ApiOperation(value = "查询未读条数", notes = "参数：{\"electricianCode\":\"ELE20241224163321\"}", response = MsgInspectResp.class)
    @RequestMapping(value = "/findUnread",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String findUnread(@RequestBody WorkOrderRequest request){
        String result;

        try {
            WorkOrderBean workOrderBean = new WorkOrderBean();
            workOrderBean.setPsElectricianCode(request.getElectricianCode());

            MsgInspectResp resp = new MsgInspectResp();

            List<WorkOrderBean> workOrderBeanList;
            workOrderBeanList = service.findUnread(workOrderBean);

            resp.setUnreadNum(workOrderBeanList.size());

            Map map = new HashMap();
            map.put("data", resp);

            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "查询成功", map);
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 一键已读
     * 服务器访问路径：http://localhost:8080/bolg/workOrder/upUnread
     * 本地访问路径：http://localhost:8111/workOrder/upUnread
     * 参数：
     * {"electricianCode":"ELE20241224163321"}
     */
    @ApiOperation(value = "一键已读", notes = "参数：{\"electricianCode\":\"ELE20241224163321\"}")
    @RequestMapping(value = "/upUnread",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String upUnread(@RequestBody WorkOrderRequest request){
        String result;

        try {
            WorkOrderBean workOrderBean = new WorkOrderBean();
            workOrderBean.setPsElectricianCode(request.getElectricianCode());

            service.upUnread(workOrderBean);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "一键已读成功", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 修改指定上报为已读
     * 服务器访问路径：http://localhost:8080/bolg/workOrder/upAppointUnread
     * 本地访问路径：http://localhost:8111/workOrder/upAppointUnread
     * 参数：
     * {"workOrderCode":"WROR20241224235227","electricianCode":"ELE20241224163321"}
     */
    @ApiOperation(value = "修改指定上报为已读", notes = "参数：{\"workOrderCode\":\"WROR20241224235227\",\"electricianCode\":\"ELE20241224163321\"}")
    @RequestMapping(value = "/upAppointUnread",method = RequestMethod.POST,produces="text/html;charset=UTF-8")
    @ResponseBody
    public String upAppointUnread(@RequestBody WorkOrderRequest request){
        String result;

        try {
            WorkOrderBean workOrderBean = new WorkOrderBean();
            workOrderBean.setPsWorkOrderCode(request.getWorkOrderCode());
            workOrderBean.setPsElectricianCode(request.getElectricianCode());

            service.upAppointUnread(workOrderBean);
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "修改已读成功", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

}
